#include "html_delegate.h"

HTMLDelegate::HTMLDelegate(QObject *parent) : QStyledItemDelegate(parent)
{
}

void HTMLDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    QStyleOptionViewItem options = option;
    initStyleOption(&options, index);

    painter->save();

    QTextDocument doc;
    // 设置默认文本颜色为白色，使用更明确的样式规则
    doc.setDefaultStyleSheet("* { color: white !important; }");
    // 在设置HTML内容之前应用样式
    doc.setHtml(QString("<div style='color: white;'>%1</div>").arg(options.text));

    options.text = "";
    options.widget->style()->drawControl(QStyle::CE_ItemViewItem, &options, painter);

    painter->translate(options.rect.left(), options.rect.top());
    QRect clip(0, 0, options.rect.width(), options.rect.height());
    doc.drawContents(painter, clip);

    painter->restore();
}

QSize HTMLDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    QStyleOptionViewItem options = option;
    initStyleOption(&options, index);

    QTextDocument doc;
    doc.setHtml(options.text);
    doc.setTextWidth(options.rect.width());
    return QSize(doc.idealWidth(), doc.size().height());
} 